﻿using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL
{
   public class SyslogDAL
    {
        #region 日志查询
        /// <summary>
        /// 日志查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<SyslogQueryModel> GetSyslogInfo(SyslogMODEL model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(@"SELECT sg.LogID,sg.Behavior,lc.TypeName,ur.Account,sg.Parameters,sg.ProcName,sg.IP,sg.CheckInTime,
sg.Exception, CASE IsException WHEN 0 THEN '正常' ELSE '异常' END AS IsException FROM dbo.Syslog AS sg
INNER JOIN dbo.LogDic AS lc ON lc.TypeID = sg.FK_TypeID
INNER JOIN dbo.[User] AS ur ON ur.UserID = sg.FK_UserID");
            List<string> listwhere = new List<string>();
            List<SqlParameter> listparm = new List<SqlParameter>();
            //操作类型
            if (model.TypeName != "0")
            {
                listwhere.Add("TypeName=@TypeName");
                listparm.Add(new SqlParameter("@TypeName", model.TypeName));
            }
            //操作人账号
            if (!string.IsNullOrWhiteSpace(model.Account))
            {
                listwhere.Add("Account=@Account");
                listparm.Add(new SqlParameter("@Account", model.Account));
            }
            //操作时间
            if (model.datein != null)
            {
                listwhere.Add("datein=@datein");
                listparm.Add(new SqlParameter("@datein", model.datein));
            }
            //至
            if (model.dateend != null)
            {
                listwhere.Add("dateend=@dateend");
                listparm.Add(new SqlParameter("@dateend", model.dateend));
            }
            //是否异常
            if (model.IsException == 0)
            {
                listwhere.Add("IsException=@IsException");
                listparm.Add(new SqlParameter("@IsException", model.IsException));
            }
            //存储过程名
            if (!string.IsNullOrWhiteSpace(model.ProcName))
            {
                listwhere.Add("ProcName=@ProcName");
                listparm.Add(new SqlParameter("@ProcName", model.ProcName));
            }
            if (listparm.Count > 0)
            {
                builder.Append($" where {string.Join(" and", listwhere.ToArray())}");
            }
            DataTable data = DBhelper.SqlDataAdapter(builder.ToString(), listparm.ToArray());
            List<SyslogQueryModel> list = new List<SyslogQueryModel>();
            foreach (DataRow row in data.Rows)
            {
                SyslogQueryModel syslo = new SyslogQueryModel()
                {
                    LogID = Convert.ToInt32(row["LogID"]),
                    Behavior = row["Behavior"].ToString(),
                    TypeName = row["TypeName"].ToString(),
                    Account = row["Account"].ToString(),
                    Parameters = row["Parameters"].ToString(),
                    ProcName = row["ProcName"].ToString(),
                    IP = row["IP"].ToString(),
                    CheckInTime = Convert.ToDateTime(row["CheckInTime"]),
                    Exception = row["Exception"].ToString()
                };
                list.Add(syslo);
            }
            return list;
        }
        #endregion


        #region 日志操作类型
        /// <summary>
        /// 日志操作类型
        /// </summary>
        /// <returns></returns>
        public DataTable GetLogDicInfo()
        {
            string sql = "SELECT * FROM dbo.LogDic";
            return DBhelper.SqlDataAdapter(sql);
        }
        #endregion
    }
}
